const {getHtmlDeatil} = require('../../util/getHtml')
const cheerio = require('cheerio')
const getItemMovieInfo = require('../../util/getItemMovieInfo')

/**
 *
 * @name getCateItemDetail
 * @description 获取当前分类接口返回信息
 * @param {htmlElement} $ 当前窗口加载节点选择
 * @param {}
 * @returns {object}
 *
 * */

function getCateItemDetail ($) {
    let data = {}
    data['list'] = []
    // 获取单个信息
    $('.index-area').find('li').each((idx,item)=>{
        let movieInfo = getItemMovieInfo($,item)
        data['list'].push(movieInfo)
    })
    let pageCountStr = $('.page').text().split('当前:')[1].slice(0,$('.page').text().split('当前:')[1].indexOf('页')).split('/')
    data['count'] = Number($('.count').text()) // 总数
    data['page_now'] = Number($('.pagenow').text())
    data['page_count'] =  Number(pageCountStr[1]) // 总数
    data['one_page_count'] = $('.index-area').find('li').length // 每页显示的条目数
    return data
}



/**
 *
 * @name getCateDetail
 * @description 获取分类下面详情页面
 *
 * */
// index-2.html 请求示例:http://localhost:8066/api/cate/detail?path=/vodtypehtml/kehuanpian/&page=3
exports.getCateDetail = async (req,res) => {
    res.set('Cache-Control','max-age=86400')
    console.log('req',req.query)
    let {path,page} = req.query
    try {
        let src = page > 1 ? `${path}index-${page}.html` : path
        console.log('src',src)
        let {data:_html} = await getHtmlDeatil(src)
        let $ = cheerio.load(_html)
        let data = getCateItemDetail($)
        res.send({
            code:200,
            msg:'获取分类详情成功',
            data
        })
    } catch (e) {
        res.send({
            code:100,
            msg:e
        })
    }
}
